<template>
    <div class="main-content">
        <TableLayout form-title="">
            <div slot="form-title" class="table-title"></div>
            <el-form :model="query" ref="query" :rules="rules" slot="form" label-width="75px" label-position="right">
                <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="班级名称" prop="schoolId">
                            <el-select v-model="query.classId" placeholder="请选择" style="width:100%" >
                                <el-option v-for="(item,index) in classData" :key="index" :label="item.sNickName" :value="item.pId"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="手机号码" prop="phone">
                            <el-input v-model="query.phone" placeholder="请输入内容" :maxlength="11"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-button type="primary" class="top-btn" @click="searchData('query')">查询</el-button>
                    </el-col>
                </el-row>
            </el-form>
             <div class="table-title" slot="table-title">
                <!-- <span class="table-title-span">查询列表</span> -->
                <div class="btnPosition">
                    <el-button @click="bethOpen">一键开通</el-button>
                </div>
            </div>
            <el-table :data="tableData" border v-loading="loading" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" :selectable="selectable">

                </el-table-column>
                <el-table-column label="学校名称" align="center">
                    <template scope="scope">
                        <span>{{scope.row.schoolName}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="商品名称" align="center">
                    <template scope="scope">
                        <span>{{scope.row.serviceName}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="卡券码" width="160" align="center">
                    <template scope="scope">
                        <span>{{scope.row.cardCoupons}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="持有人" align="center">
                    <template scope="scope">
                        <span>{{scope.row.usingName}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="手机号" align="center">
                    <template scope="scope">
                        <span>{{scope.row.phone}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="所在班级" align="center">
                    <template scope="scope">
                        <span>{{ !scope.row.className?'暂无':scope.row.className}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="使用状态" align="center">
                    <template scope="scope">
                        <span v-if=" scope.row.usingState=='0'">未使用</span>
                         <span v-else >已使用</span>
                    </template>
                </el-table-column>

                <el-table-column label="发放时间" width="170" align="center">
                    <template scope="scope">
                        <span>{{ scope.row.openTime | dateFilter}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="发放批次" align="center">
                    <template scope="scope">
                        <span>{{scope.row.batchNumber}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="开通时长" align="center">
                    <template scope="scope">
                        <span>{{ !scope.row.useFulLife?'暂无':scope.row.useFulLife+'个月'}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="操作"  width="130">
                    <template scope="scope">
                        <span v-if="scope.row.usingState=='1'">不可操作</span>                        
                        <el-button type="text" v-if="scope.row.usingState=='0'" class="btn-success" 
                        @click="openRecord(scope.row.pId)">开通</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="pageQuery.total" 
                :current-page.sync="pageQuery.currentpage" :page-sizes="config.pageSizes" 
                :page-size="config.pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange">
                </el-pagination>
            </div>
        </TableLayout>

    </div>
</template>

<script>
    import Levelbar from '../../layout/Levelbar';
    import TableLayout from 'components/TableLayout';
    import { mapState, mapActions } from 'vuex';
    import validate from '@/utils/validate';
    import { listCardCouponsGrantDetail, listClassInfoBySchoolId, openServiceByCardId } from 'api/cardCoupons';

    export default {
        name: 'extendCheckList',
        components: { Levelbar, TableLayout },
        data() {
            return { 
                classData: [],
                loading: true,  
                tableData: [],
                pageQuery: {
                    currentpage: 1,
                    total: 0
                },
                query: {
                    classId: '',
                    phone: ''

                },
                multipleSelection: [],
                rules: {
                    phone: [...validate.phone()]
                }
            }
        },
        created() {
            this.queryClass(); // 查询班级列表
            this.queryData(); // 查询列表
        },
        computed: {
            ...mapState({
                config: state => state.config.table
            })
        },
        filters: {
            dateFilter(val) {
                if (val) {
                    return val.substring(0, 19)
                } else {
                    return '--'
                }               
            }
        },
        methods: {
            ...mapActions(['ChangePageSize']),
            handleSizeChange(val) {
                this.pageQuery.currentpage = 1;
                this.ChangePageSize(val);
                this.queryData();
            },
            handleCurrentChange(val) {
                this.pageQuery.currentpage = val;
                this.queryData();
            },
            // 查询列表
            // searchData() {
            //     this.pageQuery.currentpage = 1;
            //     this.queryData();
            // },
            searchData(ruleForm) {
                this.$refs[ruleForm].validate((valid) => {
                    if (valid) {
                        this.pageQuery.currentpage = 1;
                        this.queryData();
                    } else {
                        return false;
                    }
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 查询列表
            queryData() {
                this.loading = true;
                listCardCouponsGrantDetail(
                    this.pageQuery.currentpage, 
                    this.config.pageSize,
                    this.query.phone,
                    this.query.classId,
                    '',
                    this.$route.params.id
                    ).then(response => {
                        this.tableData = response.data.content.list;
                        this.pageQuery.total = response.data.content.pagination.total;
                        this.loading = false;
                    }).catch(() => {
                        this.tableData = [];
                        this.loading = false;
                    });
            },
            // 查询班级列表
            queryClass() {               
                listClassInfoBySchoolId(this.$route.params.schoolId).then(response => {
                    this.classData = response.data.content;
                    this.classData.unshift({pId: '', sNickName: '请选择'})
                })
            },
            // 一键开通
            bethOpen() {
                let cardId =[];
                for (let i = 0; i < this.multipleSelection.length; i++) {
                    cardId.push(this.multipleSelection[i].pId)
                }
                if (cardId.length == 0) {
                    this.$message.info('请选择需要开通的卡券!')
                    return;
                }
                this.$confirm('是否确认一键开通卡券', ' ', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    this.loading = true;
                    openServiceByCardId(cardId).then(response => {
                        this.loading = false
                        this.queryData(this.pageQuery.page);
                        this.$message.success('开通卡券成功');
                    }).catch(Error => {
                        this.loading = false;
                        // this.$message.error(Error);
                    })
                }).catch(() => {
                    this.$message.info('已取消开通')
                });
            },
            // 卡券开通(卡券发放记录详情)
            openRecord(pId) {
                let cardId=[pId];
                this.$confirm('确定开通该张卡券吗？', ' ', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    openServiceByCardId(cardId).then(response => {
                        this.queryData(this.pageQuery.page);
                        this.$message.success(response.data.errorMsg);
                    })
                }).catch(() => {
                    this.$message.info('已取消开通')
                });
            },
            // 筛选框选择
            selectable(rows) {
                return rows.usingState === '0';
            }
        }

    }
</script>

<style lang="scss" scoped>

</style>

